package cn.yinyu.queue.module.ecg.dal.mysql.devrent;

import java.util.*;

import cn.yinyu.queue.framework.common.pojo.PageResult;
import cn.yinyu.queue.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.yinyu.queue.framework.mybatis.core.mapper.BaseMapperX;
import cn.yinyu.queue.module.ecg.dal.dataobject.devrent.DevRentDO;
import org.apache.ibatis.annotations.*;
import cn.yinyu.queue.module.ecg.controller.admin.devrent.vo.*;

/**
 * 装机拆机 Mapper
 *
 * @author 垠域源码
 */
@Mapper
public interface DevRentMapper extends BaseMapperX<DevRentDO> {

    default PageResult<DevRentDO> selectPage(DevRentPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<DevRentDO>()
                .eqIfPresent(DevRentDO::getDevId, reqVO.getDevId())
                .eqIfPresent(DevRentDO::getPatId, reqVO.getPatId())
                .likeIfPresent(DevRentDO::getPatName, reqVO.getPatName())
                .betweenIfPresent(DevRentDO::getRentTime, reqVO.getRentTime())
                .betweenIfPresent(DevRentDO::getReturnTime, reqVO.getReturnTime())
                .eqIfPresent(DevRentDO::getInterference, reqVO.getInterference())
                .eqIfPresent(DevRentDO::getBaseline, reqVO.getBaseline())
                .eqIfPresent(DevRentDO::getDetachment, reqVO.getDetachment())
                .eqIfPresent(DevRentDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(DevRentDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(DevRentDO::getId));
    }

    @Select("<script> " +
            "SELECT * FROM yy_queue.dev_rent " +
            "<where> " +
            "  <if test='stateList != null'> " +
            "    and state in ( " +
            "      <foreach collection='stateList' separator=',' item='state'> " +
            "        #{state} " +
            "      </foreach> ) " +
            "  </if> " +
            "  <if test=\"devId != null and devId != ''\"> " +
            "    and dev_id = #{devId} " +
            "  </if> " +
            "  <if test=\"patId != null and patId != ''\"> " +
            "    and pat_id = #{patId} " +
            "  </if> " +
            "  <if test=\"checkType != null and checkType != ''\"> " +
            "    and check_type = #{checkType} " +
            "  </if> " +
            "</where> " +
            "order by create_time desc " +
            "limit 1 " +
            "</script>")
    @Results({
            @Result(property = "patDetails", column = "pat_details", typeHandler=com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler.class)
    })
    DevRentDO getRentByState(DevRentSearchReqVO reqVO );

    @Select("select * from yy_queue.dev_rent where state=#{state} and pat_id=#{patId} and check_type=#{checkType} and to_days(create_time)=to_days(now())")
    List<DevRentDO> selectByPatAndChecktypeAndState(@Param("patId") String patId, @Param("checkType") Integer checkType, @Param("state") Integer state);

    @Select("<script>" +
            "SELECT paid FROM yy_queue.dev_rent " +
            "where id in (" +
            " <foreach collection='rentIdList' separator=',' item='rentId'>" +
            "  #{rentId} " +
            " </foreach> )" +
            "</script>")
    List<Integer> getPaidInfo(@Param("rentIdList")List<Long> rentIdList);

    @Update("update yy_queue.dev_rent set paid = #{paid} where id = #{rentId};")
    Integer setPaid(@Param("rentId") Long rentId, @Param("paid") Integer paid);
}
